package com.bii.gelapp;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.opencv.android.Utils;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class BrightnessActivity3 extends Activity {
	String analysisorigin = "";
	
	Bitmap originalBmp;
	public static Bitmap activeBmp;
	public static Bitmap colorBmp;
	Bitmap takenimg;
	Button btPrev, btMirror, btRotate;
	ImageView ivImage;
	SeekBar sbBrightness;
	
	int currentprogress;
	TextView tvBrightness;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.brightnessactivity3);
		
		if (getIntent().hasExtra("analysisorigin")) {
			analysisorigin = getIntent().getStringExtra("analysisorigin");
		}
		
		ivImage = (ImageView)findViewById(R.id.ivImage);
		sbBrightness = (SeekBar)findViewById(R.id.sbBrightness);
		tvBrightness = (TextView) findViewById(R.id.tvBrightness);
		
		
		takenimg = null;
		activeBmp = null;
		originalBmp = null;
		byte[] byteArray = getIntent().getByteArrayExtra("BMP");
		try {
			originalBmp = InternalStorage.readBitmap(this, "colorBmp");
			activeBmp = InternalStorage.readBitmap(this, "colorBmp");
			colorBmp = InternalStorage.readBitmap(this, "colorBmp");
			
			// convert bitmap to matrix
			Mat srcImg = new Mat(originalBmp.getHeight(),
					originalBmp.getWidth(), CvType.CV_8U, new Scalar(4));
			Bitmap myBitmap32 = originalBmp.copy(Bitmap.Config.ARGB_8888,
					true);
			Utils.bitmapToMat(myBitmap32, srcImg);

			// convert to grayscale
			Mat grayImg = new Mat();
			Imgproc.cvtColor(srcImg, grayImg, Imgproc.COLOR_BGR2GRAY);
			originalBmp = Bitmap.createBitmap(grayImg.cols(),
					grayImg.rows(), Bitmap.Config.RGB_565);

			// convert mat back to bitmap set originalBmp
			Utils.matToBitmap(grayImg, originalBmp);
			// set activeBmp
			activeBmp = originalBmp.copy(Bitmap.Config.RGB_565, true);
			
			//Display display = getWindowManager().getDefaultDisplay();
			//Point size = new Point();
			//display.getSize(size);
			//int screenwidth = size.x;
			//int screenheight = size.y;
			
			// activeBmp = originalBmp;
			// resized bitmap to fit image view
			/*
			Bitmap resizedbmp;
			if (activeBmp.getWidth() > activeBmp.getHeight()) {
				//if (img.getWidth() > screenwidth) {
				resizedbmp = Bitmap
							.createScaledBitmap(activeBmp, screenwidth, ((screenwidth * activeBmp
									.getHeight()) / activeBmp.getWidth()),
									true);

				//}
			} else if(activeBmp.getHeight() > activeBmp.getWidth()) {
				//if (img.getHeight() > screenheight) {
				resizedbmp = Bitmap
							.createScaledBitmap(activeBmp, ((screenheight * activeBmp
									.getWidth())/ activeBmp.getHeight()),
									screenheight, true);

				//}
			}
			else
			{
				resizedbmp = Bitmap.createScaledBitmap(activeBmp, screenwidth,
						screenwidth, true);
			}
			*/
			
			//Bitmap resizedbmp = Bitmap.createScaledBitmap(activeBmp, 300, 300,
			//		true);
			ivImage.setImageBitmap(activeBmp);
			sbBrightness.setProgress(50);
			// allow users to click buttons
			//btRotate.setEnabled(true);
			sbBrightness.setEnabled(true);
			//btMirror.setEnabled(true);
			
			sbBrightness.incrementProgressBy(1);
			sbBrightness.setProgress(50);
			sbBrightness.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

				@Override
				public void onStopTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onStartTrackingTouch(SeekBar seekBar) {
					// TODO Auto-generated method stub

				}

				@Override
				public void onProgressChanged(SeekBar seekBar, int progress,
						boolean fromUser) {
					if (takenimg == null) {
						takenimg = originalBmp;
					}
					currentprogress = sbBrightness.getProgress();
					int alpha = 1;
					int progressvalue = currentprogress;
					int beta = progressvalue - 50;


					if (alpha != 0) {
						Mat srcImg = new Mat(takenimg.getHeight(), takenimg
								.getWidth(), CvType.CV_8U, new Scalar(4));
						Bitmap myBitmap32 = takenimg.copy(Bitmap.Config.ARGB_8888,
								true);
						Utils.bitmapToMat(myBitmap32, srcImg);

						Mat destination = new Mat(srcImg.rows(), srcImg.cols(),
								srcImg.type());
						srcImg.convertTo(destination, -1, alpha, beta);
						// convert back to bitmap
						Utils.matToBitmap(destination, myBitmap32);
						activeBmp = myBitmap32;
						// set imageview and seekbar
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(myBitmap32,
						//		300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						sbBrightness.setProgress(currentprogress);
						tvBrightness.setText("Brightness: " + beta);
					} else {
						//Bitmap resizedbmp = Bitmap.createScaledBitmap(originalBmp,
						//		300, 300, true);
						//ivImage.setImageBitmap(resizedbmp);
						ivImage.setImageBitmap(activeBmp);
						sbBrightness.setProgress(currentprogress);
						tvBrightness.setText("Brightness: " + alpha);
					}

				}
			});
			
			btRotate = (Button)findViewById(R.id.btRotate);
			btRotate.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					

					try {
						//original image is now the image that has been flipped and changed brightness
						InternalStorage.writeBitmap(BrightnessActivity3.this, "originalBmp", activeBmp);
						InternalStorage.writeBitmap(BrightnessActivity3.this, "colorBmp", colorBmp);
						Intent goToNextActivity = new Intent(getApplicationContext(),
								RotationActivity.class);
						goToNextActivity.putExtra("analysisorigin", analysisorigin);
						startActivity(goToNextActivity);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}
			});
			
			btMirror = (Button)findViewById(R.id.btMirror);
			btMirror.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (originalBmp != null) {
						Matrix matrix = new Matrix();

						// matrix.preScale(-1.0f, 1.0f);
						/*
						 * if (flip == 1.0f) { flip = -1.0f; matrix.preScale(flip,
						 * 1.0f); } else { flip = 1.0f; matrix.preScale(flip, 1.0f);
						 * }
						 */

						matrix.setScale(-1.0f, 1.0f);

						if (activeBmp == null) {
							//Bitmap resizedbmp1 = Bitmap.createBitmap(originalBmp,
							//		0, 0, originalBmp.getWidth(),
							//		originalBmp.getHeight(), matrix, true);
							//Bitmap resizedbmp = Bitmap.createScaledBitmap(
							//		resizedbmp1, 300, 300, true);
							//ivImage.setImageBitmap(resizedbmp);
							//activeBmp = resizedbmp1;
							
							Bitmap flippedbmp = Bitmap.createBitmap(originalBmp,
											0, 0, originalBmp.getWidth(),
											originalBmp.getHeight(), matrix, true);
							activeBmp = flippedbmp;
							ivImage.setImageBitmap(activeBmp);
							
							// flip the original image also
							Bitmap colorBmp1 = Bitmap.createBitmap(colorBmp, 0, 0,
									colorBmp.getWidth(), colorBmp.getHeight(),
									matrix, true);
							//setcolorBmp(colorBmp1);
							colorBmp = colorBmp1;
						} else {
							/*
							Bitmap resizedbmp1 = Bitmap.createBitmap(activeBmp, 0,
									0, activeBmp.getWidth(), activeBmp.getHeight(),
									matrix, true);
							Bitmap resizedbmp = Bitmap.createScaledBitmap(
									resizedbmp1, 300, 300, true);
							ivImage.setImageBitmap(resizedbmp);
							activeBmp = resizedbmp1;
							// flip the original image also
							Bitmap colorBmp1 = Bitmap.createBitmap(colorBmp, 0, 0,
									colorBmp.getWidth(), colorBmp.getHeight(),
									matrix, true);
							colorBmp = colorBmp1;
							*/
							
							Bitmap flippedbmp = Bitmap.createBitmap(activeBmp,
									0, 0, activeBmp.getWidth(),
									activeBmp.getHeight(), matrix, true);
							activeBmp = flippedbmp;
							ivImage.setImageBitmap(activeBmp);
							
							// flip the original image also
							Bitmap colorBmp1 = Bitmap.createBitmap(colorBmp, 0, 0,
									colorBmp.getWidth(), colorBmp.getHeight(),
									matrix, true);
							//setcolorBmp(colorBmp1);
							colorBmp = colorBmp1;
						}
					}
				}
			});
			
			btPrev = (Button)findViewById(R.id.btPrev);
			btPrev.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					Intent gotoPrevActivity = new Intent(BrightnessActivity3.this, CropImageActivity.class);
					startActivity(gotoPrevActivity);
				}
			});
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
	}
	
	public byte[] compressImage(Bitmap image) {
		ByteArrayOutputStream stream = new ByteArrayOutputStream();
		image.compress(Bitmap.CompressFormat.PNG, 0, stream);
		byte[] bytes = stream.toByteArray();
		return bytes;

	}
}
